﻿////Javascript Task Object
taskDicts = new jsObjectDictionary();
function jsTask()
{
	this.progressBar = null;
	this.container = null;
	this.statusContainer = null;
	this.containerID="";
	this.status = 0;
	
	this.name = "LOL NAME"
	
	this.init = _task_init;
	this.draw = _task_draw;
	this.changeStatus = _task_changeStatus;
	this.toString = _task_toString;
	this.fromString = _task_fromString;
	
	jsTask.prototype.keys = ["status","name"];
}

function _task_init(containerID)
{
	this.containerID= containerID;
}

function _task_draw()
{
	if(!this.statusContainer)
	{
		if(!this.container)
		{
			if(this.containerID=="")
			{
				throw "No Container ID";
				return;
			}
			this.container = document.getElementById(this.containerID);
			if(!this.container)
			{
				throw "Could not find Element: " + this.containerID;
				return;
			}
		}
		this.statusContainer = document.createElement('span');
		this.statusContainer.setAttribute('id','task_' + taskDicts.count );
		this.container.appendChild(this.statusContainer);	
	}
	if(!this.progressBar)
	{
		if(!this.container)
		{
			if(this.containerID=="")
			{
				throw "No Container ID";
				return;
			}
			this.container = document.getElementById(this.containerID);
			if(!this.container)
			{
				throw "Could not find Element: " + this.containerID;
				return;
			}
		}
		this.progressBar = new jsProgressBar();
		if(!this.progressBar)
		{
			throw "Could not create Progress Bar for task";
			return;
		}
		taskDicts.add(this.progressBar,this);
		this.progressBar.init(this.container,300,15,15,1,"sans-serif",this.status);
		this.progressBar.setProgressNC(0,this.status);
		this.progressBar.progress_changed = this.changeStatus;
	}
	this.progressBar.draw();
	this.statusContainer.innerHTML = this.status;
}

function _task_changeStatus()
{
	var obj = taskDicts.getVal(this);
	obj.status = Math.floor(this.progress[1]*100)/100;
	obj.statusContainer.innerHTML = obj.status;
}

function _task_toString(key)
{
	switch(key)
	{
		case 'status':
		{
			return this.status;
		}
		break;
		case 'name':
		{
			return this.name;
		}
		break;
	}
	return '';
}

function _task_fromString(key,val)
{
	switch(key)
	{
		case 'status':
		{
			this.status = parseFloat(val);
			
		}
		break;
		case 'name':
		{
			this.name = val;
		}
		break;
	}
}